<?php

namespace App\Admin\Controllers;

use App\Models\JxtCategory;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Tree;
use Illuminate\Support\Facades\App;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;

class ArticleCategoryController extends AdminController
{
    use HasResourceActions;

    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '文章分类';

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(trans('文章分类'))
            ->row(function (Row $row) {
                $tree = new Tree(new JxtCategory());
                $tree->disableCreate();
                $tree->branch(function ($branch) {
                    $img_html = '';
                    if (!empty($branch['image'])) {
                        $img_html = "<img height='20' src='" . config('filesystems.disks.admin.url') . '/' . $branch['image'] . "'> ";
                    }
                    $payload = $img_html . "<strong>{$branch['title']}</strong>";
                    return $payload;
                });
                $row->column(6, $tree->render());
                $row->column(6, function (Column $column) {
                    $form = new \Encore\Admin\Widgets\Form();
                    $form->action(admin_base_path('article-category'));
                    $form->select('parent_id', '父类名称')->options(JxtCategory::selectOptions());
                    $form->text('title', '标题')->required()->rules('required');
                    $form->number('order', '排序')->default(0);
//                    $form->image('image', '分类图片');
//                    $form->hidden('creator_uid')->default(Admin::user()->id);
                    $form->hidden('_token')->default(csrf_token());
                    $column->append((new Box('新增分类', $form))->style('success'));
                });
            });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new JxtCategory);

        $form->tools(function (Form\Tools $tools) {
            // 去掉`查看`按钮
            $tools->disableView();
        });

        $form->text('title', __('标题'));
        $form->number('parent_id', __('父类'));
        $form->number('order', __('排序'));
        $form->image('image', __('分类图'));

        return $form;
    }

    public function getArticleCatalogTreeView()
    {
        $tree = new Tree(new JxtCategory());
        return JxtCategory::tree(function (Tree $tree) {
            $tree->disableCreate();
            $tree->branch(function ($branch) {
                $img_html = '';
                if (!empty($branch['image'])) {
                    $img_html = "<img height='20' src='" . config('filesystems.disks.admin.url') . '/' . $branch['image'] . "'> ";
                }
                $payload = $img_html . "<strong>{$branch['title']}</strong>";
                return $payload;
            });
        });
    }
}
